package skins {

   import flash.display.*;
   import flash.geom.*;
   import flash.utils.*;
   import mx.controls.Alert;
   
   import mx.skins.RectangularBorder;

   public class GradientBackgroundSkin extends RectangularBorder{
      public function GradientBackgroundSkin() {
         super();
      }

      override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void {
         
         super.updateDisplayList(unscaledWidth, unscaledHeight);
         
         var fill_type:String = GradientType.LINEAR;
         var matr:Matrix = new Matrix();
         var gradientcolors:Array = new Array();
         var alphas:Array = new Array();
         var ratios:Array = new Array();
         
         
         //get the gradient colors
         if(getStyle("gradientcolors"))
         {
         	gradientcolors = getStyle("gradientcolors");
         	 
         	//get the alphas if set, else set default to 1
         	if(getStyle("alphas"))
         	{
         		alphas = getStyle("alphas");
         	}
         	else
         	{
         		for(var i:Number=0; i<gradientcolors.length; i++) alphas.push(1);
         	}
         	
         	
         	//get the ratios if set, else set default values
         	if(getStyle("ratios"))
         	{
         		ratios = getStyle("ratios");
         	}
         	else
         	{
         		for(var j:Number=0; j<gradientcolors.length; j++) ratios.push(j*(255/gradientcolors.length));
         	}
         	
         }
         
         //check if gradient directions is set
         if(getStyle('gradientdirection'))
         {
         	var direction:String = getStyle('gradientdirection');
         	
         	if(direction.toUpperCase() == 'HORIZONTAL' ){
         		matr = horizontalGradientMatrix(0, 0, unscaledWidth, unscaledHeight);
         	}else
         		matr = verticalGradientMatrix(0, 0, unscaledWidth, unscaledHeight);
         }
		 
		 var spreadMethod:String = SpreadMethod.PAD;
		 this.graphics.beginGradientFill(fill_type, gradientcolors, alphas, ratios, matr);  
		 this.graphics.drawRect(0,0,unscaledWidth,unscaledHeight);
            
      }
   }
}